package com.boco.fznq.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.boco.fznq.Datas;
import com.boco.fznq.R;
import com.boco.fznq.view.LoopBannerView;
import com.culiu.core.fragment.BaseCoreMVPFragment;

import java.util.List;

/**
 * Created by jiangcheng on 2016/12/12.
 */

public class HomeFragment extends BaseCoreMVPFragment<HomePresenter, IHomeUI> implements IHomeUI {


    private LoopBannerView mLoopBanner;

    private GridView mGridview;

    private RecyclerView mRecycleview;

    private HomeGridAdapter mGridAdapter;

    private HomeRvAdapter mRvAdapter;

    private LinearLayoutManager linearLayoutManager;



    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        getPresenter().requestNewsData();
    }

    private void initView() {
        mLoopBanner = mViewFinder.find(R.id.loop_banner);
        mGridview = mViewFinder.find(R.id.main_gridview);
        mRecycleview = mViewFinder.find(R.id.recycleview);
        linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecycleview.setLayoutManager(linearLayoutManager);
        initData();
    }

    /**
     *
     */
    private void initData() {
        mLoopBanner.fillView(Datas.getBannerData());

        mGridAdapter = new HomeGridAdapter(getActivity(), Datas.getGridData());
        mGridview.setAdapter(mGridAdapter);
        mRvAdapter = new HomeRvAdapter(getActivity());
        mRecycleview.setAdapter(mRvAdapter);
    }





    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter();
    }

    @Override
    protected IHomeUI getUi() {
        return this;
    }

    @Override
    public void showNewsData(List<NewsBean.News> newsList) {
        if (newsList != null && !newsList.isEmpty()) {
             mRvAdapter.setDatas(newsList);
        }
    }
}
